﻿using System.Collections.Generic;
using System.Linq;
using ExcellentCRM.DataService.Models;

namespace ExcellentCRM.DataService.Implements
{
    public class DepartmentService : BaseService<Department>
    {
        public IEnumerable<Department> Search(string deptName, int pageSize, int pageIndex,
                                            out int totalCount)
        {
            using (var dbCtx = new DataServiceContext())
            {
                IQueryable<Department> searchQuery =
                    dbCtx.Departments
                         .Where(
                             p =>
                             string.IsNullOrEmpty(deptName) || p.DeptName.Contains(deptName));

                totalCount = searchQuery.Count();
                return
                    searchQuery.OrderByDescending(p => p.Id)
                               .Skip(pageSize*(pageIndex - 1))
                               .Take(pageSize)
                               .ToList();
            }
        }
    }
}